$(function(){
	//页面的调出

	var pwdModifyPwd=true;

	initEvent();
	
	function initEvent(){
		var dealPwdValue = $("#deal_old_pwdValue").val();
		if(!dealPwdValue == '' && null != dealPwdValue && "null" != dealPwdValue){
			$('#show_dealOldPwd').css('display','block');
			$('.list_left1 .txt2').html('已设置');
			$('.list_right .btn1').html('修改');
			$('#resetPayPwd').show();
			$('.list_left1 .icon1').removeClass('.icon1').addClass('icon2');
		}
	}
	
	function showpage(){
		//修改登录密码的跳出
		$('.basic_content .list1 .btn').on('click',function(){
			$('.basic_content').css('display','none');
			$('.login_change').css('display','block');
			$('.login_change .change_title .title_right').on('click',function(){
				$('.basic_content').css('display','block');
				$('.login_change').css('display','none');
			})
		})
		//设置交易密码的调出
		$('.basic_content .list2 .btn1').on('click',function(){
			$('.basic_content').css('display','none');
			$('.setting_trandpass').css('display','block');
			$('.setting_trandpass .setting_title .title_right').on('click',function(){
				$('.basic_content').css('display','block');
				$('.setting_trandpass').css('display','none');
			})
		})
		//修改交易密码的调出
		$('.basic_content .list2 .btn2').on('click',function(){
			$('.basic_content').css('display','none');
			$('.trand_change').css('display','block');
			$('.trand_change .change_title .title_right').on('click',function(){
				$('.basic_content').css('display','block');
				$('.trand_change').css('display','none');
			})
		})
		//修改绑定手机号
		$('.basic_content .list3 .btn').on('click',function(){
			$('.basic_content').css('display','none');
			$('.change_phone').css('display','block');
			$('.change_phone .setting_title .title_right').on('click',function(){
				$('.basic_content').css('display','block');
				$('.change_phone').css('display','none');
			})
		})
	}
	showpage();
	//修改登录密码的验证

	changepassword();
	function changepassword(){
		$("#login_new_pwd").blur(function() {
			var val=$(this).val();
			//var preg = /^(0|86|17951)?(13[0-9]|15[012356789]|17[0678]|18[0-9]|14[57])[0-9]{8}$/;
			if(val==""){
				$('.login_change .mistake').html('新密码不能为空');
				pwdModifyPwd=false;
			}else if(val.length<6||val.length>20){
				$('.login_change .mistake').html('请输入6-20位字符密码');
				pwdModifyPwd=false;
			}else{
				$('.login_change .mistake').html('');
				pwdModifyPwd=true;

				//	}else if(!val.match(preg)){
				//		$('.login_change .mistake').html('请输入6-20位密码,有数字、字母和符号组成');
				//		pwdModifyPwd=false;
			}

		})


		$("#login_sure_pwd").blur(function() {
			var val=$(this).val();
			var newPwd = $("#login_new_pwd").val();
			if(val != newPwd){
				$('.login_change .mistake').html('两次密码输入不一致');
				pwdModifyPwd=false;
			}else{
				$('.login_change .mistake').html('');
				pwdModifyPwd=true;
			}
		})
	}
	// 设置交易密码的流程
	function step(){
		// 到达设置交易信息页面
		$('.step_content1 .btn a').click(function(){
		var code = $("#transaction_code").val();
		if(code == ''){
			layer.msg("请输入验证码");
			return;
		}
		//交易密码的流程  校验 短信验证码是否正确	
			$.ajax({
				url:g_requestContextPath + "/user/sendMsgVerify",
				type:"post",
				data:{
					"mobile": $("#transaction_mobile").html(),
					"code" : code,
					"apiType" : 'transaction'
				},
				success:function(data){
					json = toJson(data); //转json
					//console.log('返回内容：\n'+ JSON.stringify(data));
					if(!json.result.status){
						layer.msg(json.result.errorMessage);
						return;
					}
					$('.step_content1').css('display','none');
					$('.step_content2').css('display','block');
					$('.step2').addClass('step_active');
				}
			});
		})
		//设置交易密码  到达设置成功页面
		$('.step_content2 .btn #transaction_submit').click(function(){
			var transactionPwd = true;
			var url = '/user/setDealPwd';
			var dealNewPwd = $("#deal_new_pwd").val();
			var dealSurePwd = $("#deal_sure_pwd").val();
			var oldPwd = '';
			
			var data = {
				'payPasswd' : encryptionPayPwd(dealNewPwd),
			};
			if(!$("#deal_old_pwdValue").val()==''){ 
                var dealOldPwd = $("#deal_old_pwd").val();
                if('' == dealOldPwd){
                	$('#mistake').html('旧密码不能为空');
                	return;
                }
                url = '/user/updatePwd';
                oldPwd = dealOldPwd;
                data = {
    					'newPwd'  : encryptionPayPwd(dealNewPwd),
    					"pwdType" : '1',
    					'oldPwd'  : encryptionPayPwd(oldPwd)
    			};
			} 
			
			
			
			if(dealNewPwd==""){
				$('#mistake').html('新密码不能为空');
				transactionPwd=false;
				return;
			}else if(dealNewPwd.length<6||dealNewPwd.length>20){
				$('#mistake').html('请输入6-20位字符密码');
				transactionPwd=false;
				return;
			}else if(dealNewPwd != dealSurePwd){
				$('#mistake').html('两次密码输入不一致');
				return;
			}else{
				$('#mistake').html('');
				transactionPwd=true;
			}
			$.ajax({
				url:g_requestContextPath + url,
				type:"post",
				data:data,
				success:function(data){
					//console.log('返回内容：\n'+ JSON.stringify(data));
					var json =toJson(data);
					if(json.result.status != '1'){
						layer.msg(json.result.errorMessage);
						return;
					}else{
						$('.step_content2').css('display','none');
						$('.step_content3').css('display','block');
						$('.step3').addClass('step_active');
					}
				}
			});
		})
	}
	step();
	//-----------------------------------------------------------------------------------------------
	//设置交易密码的发送验证码功能
	function dealCode(){
		$('.setting_trandpass .step_content1 .btn_active').on('click',function(){
			$(this).css('display','none');
			$('.setting_trandpass .step_content1 .btn_calm').css('display','inline-block');
			var mobile = $("#transaction_mobile").html();
			sendMsgModify(mobile,'transaction');
			time = setInterval(dealCodeFun, 1000);
		})
	}
	dealCode();
	var time = setInterval(dealCodeFun, 1000);
	var num=parseInt($('.setting_trandpass .step_content1 .btn_calm i').text());
	var dealCodeFun = function () {
		if (num <= 1) {
			num=1;
			$('.setting_trandpass .step_content1 .btn_active').css('display','inline-block');
			$('.setting_trandpass .step_content1 .btn_calm').css('display','none');
			num=60;
			clearInterval(time);
		}

		num--;
		$('.setting_trandpass .step_content1 .btn_calm i').text(num);
	}
	
	
	//---------------------------------------------------------------------------------------------
	
	
	// 修改绑定手机号的发送验证码    验证原手机号
	function code(){
		$('.change_phone .step_content1 .btn_active').on('click',function(){
			$(this).css('display','none');
			$('.change_phone .step_content1 .btn_calm').css('display','inline-block');
			var mobile = $('#binding_mobile').html();
			sendMsgModify(mobile,'bindingMobile');
			time2 = setInterval(fun, 1000);
		})

	}
	code();
	var time2 = setInterval(fun, 1000);
	var num2=parseInt($('.change_phone .step_content1 .btn_calm i').text());
	var fun = function () {
		if (num2 <= 1) {
			num2=1;
			$('.change_phone .step_content1 .btn_active').css('display','inline-block');
			$('.change_phone .step_content1 .btn_calm').css('display','none');
			num2=60;
			clearInterval(time2);
		}

		num2--;
		$('.change_phone .step_content1 .btn_calm i').text(num2);
	}
	
	//-------------------------------------------------------------------------------------------
	// 修改绑定手机号的发送验证码  验证新手机号
	function newCode(){
		$('.change_phone .step_content2 .btn_active').on('click',function(){
			$(this).css('display','none');
			$('.change_phone .step_content2 .btn_calm').css('display','inline-block');
			var newMobile = $('#new_binding_mobile').val();
			if($.trim(newMobile).length == 0){
				layer.msg("请输入新手机号");
			}else{
				sendMsgModify(newMobile,'newBindingMobile');
				timeNew = setInterval(newFun, 1000);
			}
		})

	}
	newCode();
	var timeNew = setInterval(newFun, 1000);
	var numNew=parseInt($('.change_phone .step_content2 .btn_calm i').text());
	var newFun = function () {
		if (numNew <= 1) {
			numNew=1;
			$('.change_phone .step_content2 .btn_active').css('display','inline-block');
			$('.change_phone .step_content2 .btn_calm').css('display','none');
			numNew=60;
			clearInterval(time2);
		}

		numNew--;
		$('.change_phone .step_content2 .btn_calm i').text(numNew);
	}
	
	
    /**
     * 登录密码修改
     */
	modifyPwd_submit();
	function modifyPwd_submit(){
		$('#modifyPwd_submit').on('click',function(){
			var oldPwd = $("#old_pwd").val();
			var newPwd = $("#login_new_pwd").val();
			var surePwd = $("#login_sure_pwd").val();
			if(pwdModifyPwd){
				if(oldPwd != ''&&newPwd != '' && surePwd != ''){
					$.ajax({
						url:g_requestContextPath + "/user/updatePwd",
						type:"post",
						data:{
							"newPwd":encryptionPwd(newPwd),
							"oldPwd":encryptionPwd(oldPwd),
							"pwdType" : '0'
						},
						success:function(data){
							//console.log('返回内容：\n'+ JSON.stringify(data));
							json = toJson(data);
							if(json.result.status != '1'){
								layer.msg(json.result.errorMessage);
							}else{
								//$('.basic_content').css('display','block');
								//$('.login_change').css('display','none');
								window.location.href = "/account/safetyInfo";
							}
						}
					});
				}else{
					layer.msg("请将内容输入完整");
				}
			}
		})
	}
	
	
	binding_next();
	// 修改绑定手机号	  原手机号码校验
	function binding_next(){
		// 到达设置交易信息页面
		$('.step_content1 .btn #binding_next').click(function(){
		var code = $("#mobile_code").val();
		if(code == ''){
			layer.msg("请输入验证码");
			return;
		}
		//绑定手机号  校验 短信验证码是否正确	
			$.ajax({
				url:g_requestContextPath + "/user/sendMsgVerify",
				type:"post",
				data:{
					"mobile": $("#binding_mobile").html(),
					"code" : code,
					"apiType" : 'bindingMobile'
				},
				success:function(data){
					json = toJson(data); //转json
					//console.log('返回内容：\n'+ JSON.stringify(data));
					if(!json.result.status){
						layer.msg(json.result.errorMessage);
						return;
					}
					$('.change_phone .step_content1').css('display','none');
					$('.change_phone .step_content2').css('display','block');
					$('.step2').addClass('step_active');
				}
			});
		})
	}
	
	/**
	 * 绑定新手机 号
	 */
	new_binding_next();
	function new_binding_next(){
		$('.step_content2 .btn #new_binding_next').click(function(){
			var newMobileBind = $("#new_binding_mobile").val();
			if($.trim(newMobileBind).length == 0){
				layer.msg("请输入新手机号");
				return;
			}else{
				var code = $("#new_mobile_code").val();
				if($.trim(code).length == 0){
					layer.msg("请输入验证码");
					return;
				}
			}
			
			//绑定手机号  校验 短信验证码是否正确	
				$.ajax({
					url:g_requestContextPath + "/user/updateMobile",
					type:"post",
					data:{
						"mobile": newMobileBind,
						"code" : code,
						"apiType" : 'newBindingMobile'
					},
					success:function(data){
						json = toJson(data); //转json
						//console.log('返回内容：\n'+ JSON.stringify(data));
						if(!json.result.status){
							layer.msg(json.result.errorMessage);
							return;
						}
						$('.change_phone .step_content2').css('display','none');
						$('.change_phone .step_content3').css('display','block');
						$('.step2').addClass('step_active');
					}
				});
				
			
		})
	}
})